import * as types from '@/vuex/types'
import routers from '@/router'
import axios from '@/axios/http'
import { querystring,Alert  } from 'vux'
let login =   {
    namespaced:true,
    state: {
        user: {},
        token: null,
        title: '',
        lists:null,
      },
      mutations: {
        [types.LOGIN]: (state, data) => {
            state.token = data.token
        },
        [types.LOGOUT]: (state) => {
            //localStorage.removeItem('token');
            state.token = null
        },
        [types.TITLE]: (state, data) => {
            state.title = data;
        },
        setLists(state,payload){
          state.lists = payload
        }

      },
      actions:{
        [types.LOGIN]: ({ commit, state }, values)=>{
            //console.log(values)
            if(values[1]){
              let data = {
                  passwd: values[0],
                  username: values[1],
                  token:values[2],
                }
              axios.post('/public/mxcard/Wxcard/mxLogin',querystring.stringify(data))
                .then(function (response) {
                  if(response.data.status=='200'){
                   
                    routers.replace('/listView/'+response.data.token)
                  }else{
                    Alert('账号或密码错误请重新输入')
                   // return
                  }
                })
                .catch(function (error) {
                  console.log(error);
                });
            }else{
              //alert('非法进入，确认后将跳转到首页');
              routers.replace('/WAG')
            }
            //console.log(data);
            return;
            
        },
        childLogin(state,conText){
          //console.log(conText)
          if(conText[1]){
            let data = {
                passwd: conText[0],
                username: conText[1],
                token:conText[2],
              }
            axios.post('/public/mxcard/Wxcard/childLogin',querystring.stringify(data))
              .then(function (response) {
                //登录成功返回ID，模板，用户名
                if(response.data.status=='200'){
                  routers.replace(response.data.route)
                }else{
                  Alert('账号或密码错误请重新输入')
                }
              })
              .catch(function (error) {
                console.log(error);
              });
          }else{
            //alert('非法进入，确认后将跳转到首页');
            routers.replace('/WAG')
          }
        }
      }

}
export default login;